/*
 * Copyright (C) 2018 Zhejiang xiaominfo Technology CO.,LTD.
 * All rights reserved.
 * Official Web Site: http://www.xiaominfo.com.
 * Developer Web Site: http://open.xiaominfo.com.
 */
package com.lishiots.cloud.datacenter.middleware.common;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.SQLSyntaxErrorException;
import java.util.ArrayList;
import java.util.List;

/***
 * 捕获各种异常的关键信息
 * @since:datacenter-parent 1.0
 * @author <a href="mailto:xiaoymin@foxmail.com">xiaoymin@foxmail.com</a> 
 * 2018/08/16 17:47
 */
public class SimpleExceptionMsgParser {

    private String lines;

    private String simpleMsg;
    private List<String> simpleLines=new ArrayList<>();

    public SimpleExceptionMsgParser(Exception e){
        if (e!=null){
            Throwable throwable=e.getCause();
            if (throwable instanceof SQLSyntaxErrorException){
                SQLSyntaxErrorException ssee=(SQLSyntaxErrorException)throwable;
                simpleMsg=ssee.getSQLState()+":"+ssee.getMessage();
            }else{
                simpleMsg=e.getMessage();
                StringWriter out = new StringWriter();
                e.printStackTrace(new PrintWriter(out));
                this.lines=out.toString();
                if (simpleMsg!=null){
                    String[] s=lines.split("\r\n");
                    for (String st:s){
                        simpleLines.add(st);
                    }
                }
            }
        }
    }



    public String get(){
        /*if (lines!=null){
            StringBuffer msg=new StringBuffer();
            String simple=simpleMsg;
            if (simpleLines!=null&&simpleLines.size()>2){
                simple=simpleLines.get(1);
            }
            return simple;
        }*/
        return simpleMsg;
    }


}
